<template>
  <div class="dashboard-container">
    <div class="app-container">
      <div class="edit-form">
        <el-form ref="userForm" :model="formDate" :rules="rules" label-width="220px">
          <!-- 姓名 部门 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="姓名" prop="username">
                <el-input v-model="formDate.username" size="mini" class="inputW" />
              </el-form-item>
            </el-col>

          </el-row>
          <!-- 工号 入职时间 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="工号" prop="workNumber">
                <!-- 工号自动生成，禁用组件 -->
                <el-input v-model="formDate.workNumber" disabled size="mini" class="inputW" />
              </el-form-item>
            </el-col>
          </el-row>
          <!--手机 聘用形式  -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="手机" prop="mobile">
                <!-- :disabled="!!$route.params.id" 两个！变为布尔值 -->
                <el-input
                  v-model="formDate.mobile"
                  size="mini"
                  class="inputW"
                  :disabled="!!$route.params.id"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="部门" prop="departmentId">
                <!-- 放置及联部门组件 单独封装 -->
                <selectTreeVue v-model="formDate.departmentId" class="inputW" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="聘用形式" prop="formOfEmployment">
                <el-select v-model="formDate.formOfEmployment" size="mini" class="inputW">
                  <el-option label="正式" :value="1" />
                  <el-option label="非正式" :value="2" />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="入职时间" prop="timeOfEntry">
                <el-date-picker
                  v-model="formDate.timeOfEntry"
                  size="mini"
                  type="date"
                  value-format="yyyy-MM-dd"
                  class="inputW"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="转正时间" prop="correctionTime">
                <el-date-picker
                  v-model="formDate.correctionTime"
                  size="mini"
                  type="date"
                  class="inputW"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 员工照片 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="员工头像">
                <!-- 放置上传图片 封装组件-->
                <imageUploadVue v-model="formDate.staffPhoto" />
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 保存个人信息 -->
          <el-row type="flex" justify="start">
            <el-col :span="12" style="margin-left:220px">
              <el-button size="mini" type="primary" @click="saveDate">
                {{ $route.params.id ? '保存更新' : '添加' }}
              </el-button>
            </el-col>
          </el-row>
        </el-form>
      </div>

    </div>
  </div>
</template>
<script>
import imageUploadVue from './components/image-upload.vue'
import selectTreeVue from './components/select-tree.vue'
import { addEmployee, getEmployeeDetail, updateEmployee } from '@/api/employee'

export default {
  components: {
    selectTreeVue,
    imageUploadVue
  },
  data() {
    return {
      formDate: {
        username: '', // 员工姓名
        workNumber: '', // 员工工号
        mobile: '', // 员工手机号
        departmentId: null, // 员工部门id
        formOfEmployment: null, // 聘用形式1 正式 2 非正式
        timeOfEntry: '', // 员工入职时间
        correctionTime: '', // 员工转正日期
        staffPhoto: '' // 员工头像
      },
      rules: {
        username: [
          { required: true, message: '请输入姓名', trigger: 'blur' },
          { min: 1, max: 4, message: '姓名为1-4个字符', trigger: 'blur' }
        ],

        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { pattern: /^1[3456789]\d{9}$/, message: '手机号格式错误', trigger: 'blur' }
        ],
        departmentId: [
          { required: true, message: '请选择部门', trigger: 'blur' }
        ],
        formOfEmployment: [
          { required: true, message: '请选择聘用形式', trigger: 'blur' }
        ],
        timeOfEntry: [
          { required: true, message: '请选择入职时间', trigger: 'blur' }
        ],
        correctionTime: [
          { required: true, message: '请选择转正时间', trigger: 'blur' },
          {
            trigger: 'blur',
            validator: (rule, value, callback) => {
              if (this.formDate.timeOfEntry) {
                if (new Date(this.formDate.timeOfEntry) > new Date(value)) {
                  callback(new Error('转正时间不能小于入职时间'))
                  return
                }
              }
              callback()
            }
          }
        ]
      }
    }
  },
  created() {
    // 如何获取路由参数
    // console.log(this.$route.params.id)
    if (this.$route.params.id) {
      this.getEmployeeDetail()
    }
  },
  methods: {
    saveDate() {
      this.$refs.userForm.validate(async isOk => {
        if (isOk) {
          // 判断是添加还是编辑
          if (this.$route.params.id) {
            // 编辑员工
            await updateEmployee(this.formDate)
            // 消息提示
            this.$message({
              type: 'success',
              message: '编辑成功'
            })
          } else {
            // 添加员工
            // 编辑员工
            await addEmployee(this.formDate)
            // 消息提示
            this.$message({
              type: 'success',
              message: '添加成功'
            })
          }
          // 跳转到员工列表
          this.$router.push('/employee')
          // 重置表单
          this.$refs.userForm.resetFields()
        }
      })
    },
    async getEmployeeDetail() {
      // 获取员工详情
      const result = await getEmployeeDetail(this.$route.params.id)
      this.formDate = result
    }
  }
}
</script>
<style scoped lang="scss">
    .edit-form {
      background: #fff;
      padding: 20px;
      .inputW {
        width: 380px
      }
    }

</style>
